//
//  MHCustomTableViewModel.m
//  MiaoCircle
//
//  Created by Xuzhengrong on 2018/8/17.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHCustomTableViewModel.h"
#import "MHCustomItemViewModel.h"

@implementation MHCustomTableViewModel


- (void)initialize
{
    [super initialize];
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    /// 没有数据时，隐藏底部刷新控件
    self.shouldEndRefreshingWithNoMoreData = NO ;
    
    
    @weakify(self);
    //圈子列表
    RAC(self, datas) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, datas) map:^(NSArray * circles) {
        @strongify(self)
        return [self dataSourceWithCircles:circles];
    }];
    
}



/// 请求数据
- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSArray * (^mapCircles)(NSArray *) = ^(NSArray *products) {
        if (page == 1) {
            /// 下拉刷新
        } else {
            /// 上拉加载
            products = @[(self.datas ?: @[]).rac_sequence, products.rac_sequence].rac_sequence.flatten.array;
        }
        return products;
    };
    return  [[[self customSignalWithPage:page] mh_parsedResults] map:mapCircles];
}

-(RACSignal *) customSignalWithPage:(NSInteger) page
{
    return [RACSignal empty];
}
#pragma mark - 辅助方法
- (NSArray *)dataSourceWithCircles:(NSArray *)datas {
    if (MHObjectIsNil(datas) || datas.count == 0) return @[];
    NSArray *viewModels = [datas.rac_sequence map:^(id data) {
        MHCustomItemViewModel *viewModel = [[MHCustomItemViewModel alloc] initWithModel:data];
        return viewModel;
    }].array;
    return viewModels ?: @[] ;
}
@end
